import pymysql

# 数据库连接配置
db_config = {
    'host': '192.168.10.103',
    'user': 'root',
    'password': 'root',
    'database': 'gxin',
    'charset': 'utf8mb4'
}

# 连接到 MySQL 数据库
connection = pymysql.connect(**db_config)

try:
    with connection.cursor() as cursor:
        # 更新 region 字段
        cursor.execute("UPDATE zfbider_23_info SET region = 1 WHERE region = '北京'")
        cursor.execute("UPDATE zfbider_23_info SET region = 2 WHERE region = '天津'")
        cursor.execute("UPDATE zfbider_23_info SET region = 3 WHERE region = '河北'")

        # 提交事务
        connection.commit()

    print("地区字段更新成功")
except Exception as e:
    # 回滚事务
    connection.rollback()
    print(f"更新地区字段失败，错误信息：{str(e)}")
    import traceback
    traceback.print_exc()
finally:
    # 关闭数据库连接
    connection.close()